<?php
namespace app\admin\controller;
use think\Controller;
use sand\handle;
use app\admin\model\User as UserModel;
use app\admin\model\UserAccount as UserAccountModel;
use app\admin\controller\Base;
//use think\Request;
//use think\Loader;
/**
* 
*/
class UserAccount extends Base
{
    public function index(){
    	return $this->fetch();  
    }
    public function edit(){
    	$uid=intval(input('param.uid'));
        if(!$uid) $this->error("查询参数有误");
        $user=UserModel::find($uid);
        if(!$user) $this->error("用户不存在");
        $sandConf=require CONF_PATH.'sand/config.php';
        $areas=model('area')->all(['level'=>'01']);
       /* dump(UserAccountModel::where(['uid'=>$uid])->find());
        exit;*/
        $userAccount=UserAccountModel::where(['uid'=>$uid])->find();
        $userAccount=$userAccount?$userAccount->getData():$userAccount;
        $this->assign('sandConf',$sandConf);
        $this->assign('areas',$areas);
        $this->assign('user',$user);
        $this->assign('userAccount',$userAccount);
        return $this->fetch();
    }
    public function doedit(){
        //校验
        $uid=intval($_POST['uid']);
        $accAttr=intval($_POST['accAttr']);
        $accType=intval($_POST['accType']);
        $accNo=$_POST['accNo'];
        $accName=$_POST['accName'];
        $certType=$_POST['certType'];
        $certNo=$_POST['certNo'];
        $phone=$_POST['phone'];
        $provNo=$_POST['provNo'];
        if(!$uid||!$accNo||!in_array($accAttr,[0,1])||!in_array($accType,[2,3,4]))
            $this->error("表单填写不完整");
        if(!db('user')->find($uid))
            $this->error("用户不存在");
        //认证
        $params = require_once CONF_PATH.'sand/config/params.php';
        $RealNameVerify=$params['RealNameVerify'];
        $RealNameVerify['pt']['accAttr']=$accAttr;
        $RealNameVerify['pt']['accType']=$accType;
        $RealNameVerify['pt']['accNo']=$accNo;
        $RealNameVerify['pt']['accName']=$accName;
        $RealNameVerify['pt']['certType']=$certType;
        $RealNameVerify['pt']['certNo']=$certNo;
        $RealNameVerify['pt']['phone']=$phone;
        $cert = require_once CONF_PATH.'sand/config/cert.php';
        $handle = new handle($cert);
        $res=json_decode($handle->execute($RealNameVerify));
       /* dump($RealNameVerify);
        exit;*/
        //存储
        if($res->respCode=="0000"){
            if($res->validateStatus=="0"){
                $userAcc=UserAccountModel::get(['uid'=>$uid]);
                if(!$userAcc) {$userAcc=new UserAccountModel;$userAcc->uid=$uid;}
                $userAcc->status = 1;
                $userAcc->acc_attr = $accAttr;
                $userAcc->acc_type = $accType;
                $userAcc->acc_no = $accNo;
                $userAcc->acc_name = $accName;
                $userAcc->cert_type = $certType;
                $userAcc->cert_no = $certNo;
                $userAcc->phone = $phone;
                $userAcc->prov_no=$provNo;
                /* dump($userAcc->getdata());
                exit();*/
                if($userAcc->save())
                  $this->success("操作成功");
                else $this->error("操作失败");
            }else{
                $this->error("认证不通过");
                /*dump($RealNameVerify);
                dump($res);*/
            }
        }else{
           /* $this->error($res->respCode.$res->respDesc);*/
            dump($RealNameVerify);
                dump($res);
        } 
    }
}